package pl.edu.agh.uhm.serveragent.devices.vm;

import java.util.Dictionary;
import java.util.HashSet;
import java.util.List;
import java.util.Properties;
import java.util.Random;
import java.util.Set;

import org.osgi.framework.BundleContext;
import org.osgi.service.upnp.UPnPDevice;
import org.osgi.service.upnp.UPnPIcon;
import org.osgi.service.upnp.UPnPService;
import org.apache.felix.upnp.extra.util.UPnPEventNotifier;

public class VMDevice implements UPnPDevice{

	
	final private String DEVICE_ID;
	final public static String DEVICE_ID_PREFIX="uuid:VritualMachine:";
	
	private VMService vmService;
	
	private UPnPService[] services;
	private Dictionary dictionary;

	public static UPnPEventNotifier notifier = null;

	public Dictionary getDictionary() {
		
		return dictionary;
		
	}


	

	
	public VMDevice(BundleContext context, String vmUDN,String parentUDN) {
		
		vmService=new VMService(this);	
		services = new UPnPService[]{vmService};
		DEVICE_ID=DEVICE_ID_PREFIX+vmUDN;

		initDeviceProperties(parentUDN);	
		notifier = new UPnPEventNotifier(context,this,vmService);

		
		
		
		
	}

	private void initDeviceProperties(String parentUDN) {
		
		dictionary=new Properties();
		dictionary.put(UPnPDevice.UPNP_EXPORT, "");
		dictionary.put(
		        org.osgi.service.device.Constants.DEVICE_CATEGORY,
	        	new String[]{UPnPDevice.DEVICE_CATEGORY}
	    );
		dictionary.put(UPnPDevice.FRIENDLY_NAME,"Felix OSGi-UPnP Toy");
		dictionary.put(UPnPDevice.MANUFACTURER,"AGH UHD");
		dictionary.put(UPnPDevice.MANUFACTURER_URL,"http://agh.edu.pl/pp/uhd");
		dictionary.put(UPnPDevice.MODEL_DESCRIPTION,"UPnP Device representing VM");
		dictionary.put(UPnPDevice.MODEL_NAME,"Virtual Machine");
		dictionary.put(UPnPDevice.MODEL_NUMBER,"1.0");
		dictionary.put(UPnPDevice.MODEL_URL,"http://agh.edu.pl/pp/uhd/VMhypervisor.html");
		dictionary.put(UPnPDevice.SERIAL_NUMBER,"123456789");
		dictionary.put(UPnPDevice.TYPE,"urn:schemas-upnp-org:device:VirtualMachine:1");
		dictionary.put(UPnPDevice.UDN,DEVICE_ID);
		dictionary.put(UPnPDevice.UPC,"1213456789");
		dictionary.put(UPnPDevice.PARENT_UDN,parentUDN);
			
		
		Set types = new HashSet(services.length+5);
		String[] ids = new String[services.length];
		for (int i = 0; i < services.length; i++) {
			ids[i]=services[i].getId();
			types.add(services[i].getType());
		}
		
		dictionary.put(UPnPService.TYPE, types.toArray(new String[]{}));
		dictionary.put(UPnPService.ID, ids);
		
		
		
	}
	
	public Dictionary getDescriptions(String arg0) {
		return dictionary;
	}

	
	public UPnPIcon[] getIcons(String arg0) {
		UPnPIcon icon = new VMIcon();
		return new UPnPIcon[]{icon} ;
	}

	
	public UPnPService getService(String serviceId) {
		if  (serviceId.equals(vmService.getId())) return vmService;
		else return null;
	}

	
	public UPnPService[] getServices() {
		return services;
	}

	
}
